#include <iostream>
using namespace std;
#include "sys/thread.h"
using namespace tdl;

int n = 10000000;
usec_t s, e;

template <typename Tlock>
double test(Tlock& l, bool is_wlock)
{
	sync_door<Tlock> door(l);
	s = get_usec(get_now_utime());
	for(int i = 0; i < n; ++i)
	{
		door.open(is_wlock);
		door.close();
	}
	e = get_usec(get_now_utime());

	return (double)(e - s) / (double)n;
}

int main(int argc, char* argv[])
{
	mutex_lock ml;
	double time_unit = test(ml, true);
	cout << "mutex_lock can do lock&unlock " << 
		   (int)(1000000.0 / time_unit / 10000) << "w times/s" << endl;

	rw_lock rwl;
	time_unit = test(rwl, true);
	cout << "rw_lock can do wlock&unlock " << 
		   (int)(1000000.0 / time_unit / 10000) << "w times/s" << endl;

	time_unit = test(rwl, false);
	cout << "rw_lock can do rlock&unlock " << 
		   (int)(1000000.0 / time_unit / 10000) << "w times/s" << endl;

	spin_lock sl;
	time_unit = test(sl, true);
	cout << "sl_lock can do lock&unlock " << 
		   (int)(1000000.0 / time_unit / 10000) << "w times/s" << endl;


	return 0;
}

